package com.ngovanhau.justsort.model;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.entity.modifier.DelayModifier;
import org.anddev.andengine.entity.modifier.MoveXModifier;
import org.anddev.andengine.entity.modifier.ParallelEntityModifier;
import org.anddev.andengine.entity.modifier.ScaleModifier;
import org.anddev.andengine.entity.modifier.SequenceEntityModifier;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.util.modifier.IModifier;
import org.anddev.andengine.entity.IEntity;

import android.content.Context;

import com.ngovanhau.justsort.MainGameActivity;
import com.ngovanhau.justsort.MainMenuActivity;
import com.ngovanhau.justsort.MySprite;
import com.ngovanhau.justsort.control.ValueControl;
import com.ngovanhau.justsort.item.config.MyConfig;

/*
 * 2-3: smart
 * 4-5: good job
 * 6-7: amazing
 * 8-9: incredible
 */
public class MyBonus extends MySprite {

	private BitmapTextureAtlas smart_BTA;
	private TextureRegion smart_TR;
	
	private BitmapTextureAtlas good_job_BTA;
	private TextureRegion good_job_TR;
	
	private BitmapTextureAtlas excellent_BTA;
	private TextureRegion excellent_TR;
	
	private BitmapTextureAtlas amazing_BTA;
	private TextureRegion amazing_TR;
	
	private BitmapTextureAtlas incredible_BTA;
	private TextureRegion incredible_TR;

	int time = 0;

	boolean isStop = false;

	private final int HEIGHT_IMAGE = 100;
	private final int WIDTH_IMAGE = 480;
	
	public void onLoadResources(Engine engine, Context context) {
		this.engine = engine;
		this.context = context;
		this.mainGame = (MainGameActivity) context;
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("images/bonus/");

		//smart
        this.smart_BTA = new BitmapTextureAtlas(512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        smart_TR = BitmapTextureAtlasTextureRegionFactory.createFromAsset( this.smart_BTA, context, "smart.png", 0, 0);
        engine.getTextureManager().loadTexture(this.smart_BTA);
		
		//good_job
        this.good_job_BTA = new BitmapTextureAtlas(512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        good_job_TR = BitmapTextureAtlasTextureRegionFactory.createFromAsset( this.good_job_BTA, context, "good_job.png", 0, 0);
        engine.getTextureManager().loadTexture(this.good_job_BTA);
        
        this.excellent_BTA = new BitmapTextureAtlas(512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        excellent_TR = BitmapTextureAtlasTextureRegionFactory.createFromAsset( this.excellent_BTA, context, "excellent.png", 0, 0);
        engine.getTextureManager().loadTexture(this.excellent_BTA);
		
		//amazing
        this.amazing_BTA = new BitmapTextureAtlas(512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        amazing_TR = BitmapTextureAtlasTextureRegionFactory.createFromAsset( this.amazing_BTA, context, "amazing.png", 0, 0);
        engine.getTextureManager().loadTexture(this.amazing_BTA);
		
		//incredible
        this.incredible_BTA = new BitmapTextureAtlas(512, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        incredible_TR = BitmapTextureAtlasTextureRegionFactory.createFromAsset( this.incredible_BTA, context, "incredible.png", 0, 0);
        engine.getTextureManager().loadTexture(this.incredible_BTA);
	}

	public void onLoadScene(Scene scene) {
		this.scene = scene;
	}
	
	private void add(int value){
		TextureRegion texture;
		ValueControl.isTouchNumber = false;

		switch(value){
		case 0:
			texture = smart_TR;
			break;
		case 1:
			texture = good_job_TR;
			break;
		case 2:
			texture = excellent_TR;
			break;
		case 3:
			texture = amazing_TR;
			break;
		case 4:
			texture = incredible_TR;
		default:
			texture = smart_TR;
			break;
		}
		
		float h = (int) (HEIGHT_IMAGE * MyConfig.RACE_HEIGHT);
		float w = (int) (WIDTH_IMAGE * MyConfig.RACE_WIDTH);
		float y = (int) (MyConfig.SCREENHEIGHT - h)/2;
 
		float x_start = - w/2;
		float x_between = 0;
		float x_end = w/2;
		
        final Sprite sprite = new Sprite(x_start, y, w, h, texture);
        this.scene.attachChild(sprite);
        sprite.setZIndex(101);
        scene.sortChildren();
        
        ScaleModifier mScaleModifier_in = new ScaleModifier(0.25f, 0f, 1f);
        MoveXModifier moveModifierFirst = new MoveXModifier(0.25f, x_start,x_between);
        ParallelEntityModifier mParallelEntityModifier_in = new ParallelEntityModifier(
        		mScaleModifier_in, moveModifierFirst);
        
        ScaleModifier mScaleModifier_out = new ScaleModifier(0.25f, 1f, 0f);
        MoveXModifier moveModifierSecond = new MoveXModifier(0.25f, x_between, x_end);
        ParallelEntityModifier mParallelEntityModifier_out = new ParallelEntityModifier(
        		mScaleModifier_out, moveModifierSecond);
        
        DelayModifier mDelayModifier = new DelayModifier(0.5f);
        
        SequenceEntityModifier mSequenceEntityModifier = new SequenceEntityModifier(
				new SequenceEntityModifier.ISubSequenceShapeModifierListener() {
					@Override
					public void onSubSequenceStarted(
							IModifier<IEntity> pModifier, IEntity pItem,
							int pIndex) {
					}

					@Override
					public void onSubSequenceFinished(
							IModifier<IEntity> pModifier, IEntity pItem,
							int pIndex) {
						if (pIndex == 2) {
							removeAnimatedSprite(sprite);
							ValueControl.isTouchNumber = true;
						}
					}
				}, mParallelEntityModifier_in, mDelayModifier,
				mParallelEntityModifier_out);
        sprite.registerEntityModifier(mSequenceEntityModifier);
        MainMenuActivity.sound.playBonus(value);
	}
	
	public void removeAnimatedSprite(final Sprite sprite) {
        engine.runOnUpdateThread(new Runnable() {
            @Override
            public void run() {
                scene.detachChild(sprite);
            }
        });
    }

	public void startAnimation(int bonus){
		if(bonus >= 10){
			add(4);
		}else if(bonus >= 8){
			add(3);
		}else if(bonus >= 6){
			add(2);
		}else if(bonus >= 4){
			add(1);
		}else if(bonus >= 2){
			add(0);
		}
	}
}
